package main

import (
	"bytes"
	"fmt"
	"github.com/vektah/gqlparser/v2/ast"
	"github.com/vektah/gqlparser/v2/formatter"
	"github.com/vektah/gqlparser/v2/parser"
	"log"
	"toGraphqSDL/util"
)

func main() {
	name := "test"
	/*bs, err := ioutil.ReadFile("cmd/schema.graphql")
	if err != nil {
		log.Fatal(err)
	}*/
	bs, err := util.DownloadSdl("http://192.168.3.132:8081/v1/graphql")
	if err != nil {
		log.Fatal(err)
	}

	doc, gqlErr := parser.ParseSchema(&ast.Source{
		Name:  name,
		Input: bs,
	})
	if gqlErr != nil {
		log.Fatal(gqlErr)
	}
	for _, define := range doc.Definitions {

		switch define.Kind {
		case ast.Object:
			fmt.Println(define)

			//define.Name = "a_" + define.Name

		case ast.Enum:

		case ast.InputObject:

		}
	}

	// exec format
	var buf bytes.Buffer
	f := formatter.NewFormatter(&buf)
	f.FormatSchemaDocument(doc)

	// validity check
	doc, gqlErr = parser.ParseSchema(&ast.Source{
		Name:  name,
		Input: buf.String(),
	})
	//fmt.Println(buf.String())
	if gqlErr != nil {
		fmt.Println(buf.String())
		log.Fatal(gqlErr)
	}
}
